import { defineStore } from 'pinia'
import { loginAPI } from '@/apis/user'
import { ref } from 'vue'
import { useCartStore } from './cart'


export const useUserStore = defineStore('user', () => {
  const userInfo = ref({})
  const cartStore = useCartStore()
  const setUserInfo = async ({ account, password }) => {
    const res = await loginAPI({ account, password })
    if (res.code === '1'){
      userInfo.value = res.result
    }
    await cartStore.mergeCart()
  }
const clearUserInfo = () => {
  cartStore.clearCart()
  userInfo.value = {}
}

  return {
    userInfo,
    setUserInfo,
    clearUserInfo
   }
}, {
  persist: true
})
